/**
 * Chesney's Fireplace System Includes New Supplier, New Fireplace Item, Supplier and Fireplace Update, 
 * Supplier and Fireplace Reports, Login, main menu and Tutorial.
 * 
 * It also Includes Database connection, Database Extension and the two classes which are MySupplier and My Fireplace.
 *  
 * Copyright (C) Bournemouth University 
 * 
 * @author Ruth Shepperd, Ari Lopez
 * 
 * @version 1.0
 */
package GUI;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.print.PrinterException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.BevelBorder;
import javax.swing.table.DefaultTableModel;
import Other.Glob;
import database.DatabaseExtension;
import database.MyAddressee;
import database.MyClient;
import database.MyCountry;
import database.MyDestination;
import database.MyIncoterm;
import database.MyPayment;
import database.MyShipment;
import database.MySupplier;
import database.MyTax;
import database.MyTransport;
import database.NewTirDataBase;
import enums.SearchType;
import javax.swing.ScrollPaneConstants;
import javax.xml.bind.annotation.XmlElementDecl.GLOBAL;
/**
* Class Report.
* This Class will be used to create a table that will contain the data from 
* the main two tables from the database fireplace and supplier. The report
* generated can be printed by the user.
* 
* @author Ari Lopez
* 
* @version 1.0
* 
*/
public class Report extends JFrame {
	 // elements of the screen
     private JPanel northPanel = new JPanel();   
     private JPanel southPanel = new JPanel();
     private JScrollPane scrollPane;
     private JLabel labelTitle;
     private JButton printButton;
     private JTextField tfSelect;
     private JButton btnEnter;
  	 private JButton btnEscExit;
     // variable to handle this window frame
     private JFrame thisFrame = this;
     //title of the screen
     private String title;
     //table that holds the data
     private JTable table;
     //set up default model
     DefaultTableModel model;
    //variable that holds the ids of the 
 	private ArrayList<Integer> ids;
 	private ArrayList<String> id;
	/**
      * Constructor Method to set the query and the title of the report
      */
     public Report(){
    	setTitle();
    	//System.out.println("this is the model in the report constructor" + model);
		System.out.println("Report Constructor has been called");
			southPanel.setSize(800, 100);
	        System.out.println("this is the model in the constructor " + model);
	        
	        
		    table = new JTable(model);
		   
	        scrollPane = new JScrollPane(table);
	        
	      //setting visibility true
	        setVisible(true);  
	        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	        setResizable(false);
	        setLocation(800,150);
	        JPanel panel_1 = new JPanel();
	        panel_1.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
	        GroupLayout gl_northPanel = new GroupLayout(northPanel);
	        gl_northPanel.setHorizontalGroup(
	        	gl_northPanel.createParallelGroup(Alignment.LEADING)
	        		.addComponent(scrollPane, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, 794, Short.MAX_VALUE)
	        		.addGroup(gl_northPanel.createSequentialGroup()
	        			.addGap(246)
	        			.addComponent(panel_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	        			.addContainerGap(259, Short.MAX_VALUE))
	        );
	        gl_northPanel.setVerticalGroup(
	        	gl_northPanel.createParallelGroup(Alignment.TRAILING)
	        		.addGroup(gl_northPanel.createSequentialGroup()
	        			.addContainerGap()
	        			.addComponent(panel_1, GroupLayout.DEFAULT_SIZE, 48, Short.MAX_VALUE)
	        			.addPreferredGap(ComponentPlacement.RELATED)
	        			.addComponent(scrollPane, GroupLayout.PREFERRED_SIZE, 402, GroupLayout.PREFERRED_SIZE)
	        			.addContainerGap())
	        );
	        labelTitle = new JLabel(title);
	        labelTitle.setFont(new Font("Tahoma", Font.BOLD, 18));
	        GroupLayout gl_panel_1 = new GroupLayout(panel_1);
	        gl_panel_1.setHorizontalGroup(
	        	gl_panel_1.createParallelGroup(Alignment.LEADING)
	        		.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
	        			.addContainerGap(149, Short.MAX_VALUE)
	        			.addComponent(labelTitle, GroupLayout.PREFERRED_SIZE, 267, GroupLayout.PREFERRED_SIZE)
	        			.addGap(65))
	        );
	        gl_panel_1.setVerticalGroup(
	        	gl_panel_1.createParallelGroup(Alignment.LEADING)
	        		.addGroup(Alignment.TRAILING, gl_panel_1.createSequentialGroup()
	        			.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
	        			.addComponent(labelTitle)
	        			.addContainerGap())
	        );
	        panel_1.setLayout(gl_panel_1);
	        northPanel.setLayout(gl_northPanel);
	        GroupLayout groupLayout = new GroupLayout(getContentPane());
	        groupLayout.setHorizontalGroup(
	        	groupLayout.createParallelGroup(Alignment.LEADING)
	        		.addComponent(northPanel, GroupLayout.DEFAULT_SIZE, 794, Short.MAX_VALUE)
	        		.addGroup(groupLayout.createSequentialGroup()
	        			.addGap(10)
	        			.addComponent(southPanel, GroupLayout.DEFAULT_SIZE, 774, Short.MAX_VALUE)
	        			.addContainerGap())
	        );
	        groupLayout.setVerticalGroup(
	        	groupLayout.createParallelGroup(Alignment.LEADING)
	        		.addGroup(groupLayout.createSequentialGroup()
	        			.addComponent(northPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	        			.addPreferredGap(ComponentPlacement.RELATED, 20, Short.MAX_VALUE)
	        			.addComponent(southPanel, GroupLayout.PREFERRED_SIZE, 94, GroupLayout.PREFERRED_SIZE)
	        			.addContainerGap())
	        );
	        printButton = new JButton("F10 Print");
	        //set up F10 for printing
	        printButton.getInputMap(printButton.WHEN_IN_FOCUSED_WINDOW)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_F10, 0), "");
	        printButton.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					 try {
							table.print();
						} catch (PrinterException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
				    }
			});
	        printButton.addActionListener(new ActionListener() {
	        	//set up button to print table
	        	public void actionPerformed(ActionEvent e) {
					try {
						//method to print the table
						table.print();
					} catch (PrinterException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
	        	}
	       });
	        JLabel selectLabel = new JLabel("Select ID number");
	        tfSelect = new JTextField();
	        //set up escape to exit the window
	    	tfSelect.getInputMap(tfSelect.WHEN_IN_FOCUSED_WINDOW)
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "");
			tfSelect.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					 //calling the method
					escape(Glob.type);
				    }
			});
	        tfSelect.addKeyListener(new KeyAdapter() {
				@Override
				public void keyTyped(KeyEvent e) {
					//when a number is entered
					if(e.getKeyChar() == KeyEvent.VK_ENTER){
						System.out.println("This is the data type in report when I presse the button" + Glob.type);
						enterData(Glob.type);
					}
				}
			});
	        tfSelect.setColumns(10);
	        btnEnter = new JButton("Enter ");
	        //set up button to enterdata on the proper object screen
	        btnEnter.addActionListener(new ActionListener() {
	        	public void actionPerformed(ActionEvent e) {
	        		enterData(Glob.type);
	        	}
	        });
	        btnEscExit = new JButton("Esc Exit");
	        btnEscExit.getInputMap(btnEscExit.WHEN_IN_FOCUSED_WINDOW)
	        //set up escape for exit
			.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "");
	        btnEscExit.getActionMap().put("", new AbstractAction() {
				 public void actionPerformed(ActionEvent e) {
					 escape(Glob.type);
				    }
			});
	        btnEscExit.addActionListener(new ActionListener() {
	        	//set up exit button for exit
	        	public void actionPerformed(ActionEvent e) {
	        		escape(Glob.type);
	        	}
	        });
	        GroupLayout gl_southPanel = new GroupLayout(southPanel);
	        gl_southPanel.setHorizontalGroup(
	        	gl_southPanel.createParallelGroup(Alignment.TRAILING)
	        		.addGroup(gl_southPanel.createSequentialGroup()
	        			.addGap(43)
	        			.addComponent(btnEscExit)
	        			.addPreferredGap(ComponentPlacement.RELATED, 168, Short.MAX_VALUE)
	        			.addComponent(selectLabel)
	        			.addPreferredGap(ComponentPlacement.UNRELATED)
	        			.addComponent(tfSelect, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	        			.addGap(44)
	        			.addComponent(btnEnter)
	        			.addGap(18)
	        			.addComponent(printButton, GroupLayout.PREFERRED_SIZE, 98, GroupLayout.PREFERRED_SIZE)
	        			.addGap(53))
	        );
	        gl_southPanel.setVerticalGroup(
	        	gl_southPanel.createParallelGroup(Alignment.TRAILING)
	        		.addGroup(gl_southPanel.createSequentialGroup()
	        			.addContainerGap(38, Short.MAX_VALUE)
	        			.addGroup(gl_southPanel.createParallelGroup(Alignment.BASELINE)
	        				.addComponent(printButton)
	        				.addComponent(selectLabel)
	        				.addComponent(tfSelect, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
	        				.addComponent(btnEnter)
	        				.addComponent(btnEscExit))
	        			.addGap(33))
	        );
	        southPanel.setLayout(gl_southPanel);
	        getContentPane().setLayout(groupLayout);
	        setSize(996, 631); //setting frame size
	        //request focus when the screen is open
	        tfSelect.requestFocus(true);
	        //screen not resizable
	        setResizable(false);
	        //System.out.println("report was open again");
	}
     //method that returns a DefaultTableModel to populate the table
     //it takes a SQL query as parameter
 public  DefaultTableModel buildTableModel(String query) {
	 	ids = new ArrayList<Integer>();
		Vector<String> columnNames = null;
		Vector<Vector<Object>> data = null;
		 try{
		 Class.forName("com.mysql.jdbc.Driver").newInstance();
		    //connect with the database
		 Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/newtir", "root", "");
	    // The Connection is obtained
		Statement stmt = (Statement) con.createStatement();
	    ResultSet rs = stmt.executeQuery(query);
	   // System.out.println("this is the string got from search Bar " + s);
	    ResultSetMetaData metaData = (ResultSetMetaData) rs.getMetaData();
	    // names of columns
	    columnNames = new Vector<String>();
	    int columnCount = metaData.getColumnCount();
	    for (int column = 1; column <= columnCount; column++) {
	        columnNames.add(metaData.getColumnName(column));
	    }
	    // data of the table
	    data = new Vector<Vector<Object>>();
	    while (rs.next()) {
	        Vector<Object> vector = new Vector<Object>();
	        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
	        	Object ss  =rs.getObject(columnIndex);
	            vector.add(ss);
	            //System.out.println("Iterating");
	           if (columnIndex==1){
	        	   //System.out.println("This is the buidlTAblemethod adding column indexes to an array" + ss);
	            	ids.add(Integer.valueOf(ss.toString()));
	           }
	        }
	        data.add(vector);
	    }
	    rs.close();
	    stmt.close();
	    con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	    return new DefaultTableModel(data, columnNames);
	}
 
 public  DefaultTableModel buildTableModelString(String query) {
	 	id = new ArrayList<String>();
		Vector<String> columnNames = null;
		Vector<Vector<Object>> data = null;
		 try{
		 Class.forName("com.mysql.jdbc.Driver").newInstance();
		    //connect with the database
		 Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/newtir", "root", "");
	    // The Connection is obtained
		Statement stmt = (Statement) con.createStatement();
	    ResultSet rs = stmt.executeQuery(query);
	   // System.out.println("this is the string got from search Bar " + s);
	    ResultSetMetaData metaData = (ResultSetMetaData) rs.getMetaData();
	    // names of columns
	    columnNames = new Vector<String>();
	    int columnCount = metaData.getColumnCount();
	    for (int column = 1; column <= columnCount; column++) {
	        columnNames.add(metaData.getColumnName(column));
	    }
	    // data of the table
	    data = new Vector<Vector<Object>>();
	    while (rs.next()) {
	        Vector<Object> vector = new Vector<Object>();
	        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
	        	Object ss  =rs.getObject(columnIndex);
	            vector.add(ss);
	            //System.out.println("Iterating");
	           if (columnIndex==1){
	        	   //System.out.println("This is the buidlTAblemethod adding column indexes to an array" + ss);
	            	id.add(ss.toString());
	           }
	        }
	        data.add(vector);
	    }
	    rs.close();
	    stmt.close();
	    con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	    return new DefaultTableModel(data, columnNames);
	}
 //method to get the textfield in other screens
 public String getFieldText() {
	    return tfSelect.getText();
	 }
 //method to start the data
 public void enterData(SearchType type){
	 System.out.println("This is the search type in the method enter data inside the report" + type);
			if(!tfSelect.getText().isEmpty()){
			try{
					boolean ok = false;
					if (type == SearchType.TRANSPORT || type == SearchType.ADDSHIPMENTTRANSPORT){
						for (String iterate: id) {
							if (iterate.equalsIgnoreCase(tfSelect.getText()))
									ok=true;
							System.out.println(id);
						}
						if (!ok) {
							JOptionPane.showMessageDialog(null, "Choose an ID from the table");
							tfSelect.setText("");
							return;
						}
						
					}else{
					for (int iterate: ids) {
						if (iterate == Integer.parseInt(tfSelect.getText()))
								ok=true;
						System.out.println(ids);
					}
					if (!ok) {
						JOptionPane.showMessageDialog(null, "Choose an ID from the table");
						tfSelect.setText("");
						return;
					}
					}
					if (type ==  SearchType.COUNTRY){
						System.out.println("this is the id got from country" + tfSelect.getText());
			NewTirDataBase country = (MyCountry) DatabaseExtension.getTableID( "SELECT * FROM Country WHERE CountryID = '" + tfSelect.getText() +"'")[0];
			System.out.println("This is the country database object" + country);
			Country c = Country.getInstance();
			c.init((MyCountry)country);
			SearchWindow s = SearchWindow.getInstance();
			s.enableSearchWindow();
			s.dispose();
			thisFrame.dispose();
			c.enableCountry();
				//System.out.println("this the country id in report " + ((MyCountry) country).getCountryID());
					}
			else if (type ==  SearchType.PAYMENT){
			NewTirDataBase payment = (MyPayment) DatabaseExtension.getTableID("SELECT * FROM Payment WHERE PaymentID = '" + tfSelect.getText() +"'")[0];		
			Payment p = Payment.getInstance();
			p.getInstance().init((MyPayment) payment);
			SearchWindow s = SearchWindow.getInstance();
			s.enableSearchWindow();
			s.dispose();
			thisFrame.dispose();
			p.enablePayment();
			//System.out.println("this the payment id in report " + ((MyPayment) payment).getPaymentID());
					}
			else if (type ==  SearchType.INCOTERM){
				NewTirDataBase incoterm = (MyIncoterm) DatabaseExtension.getTableID("SELECT * FROM Incoterm WHERE IncotermID = '" + tfSelect.getText() +"'")[0];		
				Incoterm i = Incoterm.getInstance();
				i.getInstance().init((MyIncoterm) incoterm);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				i.enableIncoterm();
						}
					
			else if (type ==  SearchType.TAX){
				NewTirDataBase tax = (MyTax) DatabaseExtension.getTableID("SELECT * FROM tax WHERE TaxID = '" + tfSelect.getText() +"'")[0];		
				Tax t = Tax.getInstance();
				t.getInstance().init((MyTax) tax);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				t.enableTax();
						}
			else if (type ==  SearchType.ADDRESSEE){
				
				
				//String query = "Select AddresseeID, addressee.name , address, postcode, town, province, addressee.countryID, country.name, phonenumber, fax, nif, contact, email from addressee, country where addressee.countryid = country.countryID and addresseeID = '" + tfSelect.getText() +"'";
				String query = "SELECT * FROM addressee WHERE addresseeID = '" + tfSelect.getText() +"'";
				System.out.println(query);
				System.out.println("This is the report for addressee");
				NewTirDataBase addressee = (MyAddressee) DatabaseExtension.getTableID(query)[0];		
				Addressee a = Addressee.getInstance();
				a.init((MyAddressee) addressee);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				a.enableAddressee();
						}
					
			else if (type ==  SearchType.ADDRESSEECOUNTRY){
				NewTirDataBase country = (MyCountry) DatabaseExtension.getTableID("SELECT * FROM country WHERE CountryID = '" + tfSelect.getText() +"'")[0];		
				Addressee a = Addressee.getInstance();
				a.initCountry((MyCountry) country);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				a.enableAddressee();
				
						}
			else if (type ==  SearchType.SUPPLIER){
							
							
				//String query = "Select AddresseeID, addressee.name , address, postcode, town, province, addressee.countryID, country.name, phonenumber, fax, nif, contact, email from addressee, country where addressee.countryid = country.countryID and addresseeID = '" + tfSelect.getText() +"'";
				String query = "SELECT * FROM supplier WHERE supplierID = '" + tfSelect.getText() +"'";
				//System.out.println(query);
				//System.out.println("This is the report for addressee");
				NewTirDataBase supplier = (MySupplier) DatabaseExtension.getTableID(query)[0];		
				Supplier su = Supplier.getInstance();
				su.init((MySupplier) supplier);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				su.enableSupplier();
				
						}
			else if (type ==  SearchType.SUPPLIERCOUNTRY){
				
	
				NewTirDataBase country = (MyCountry) DatabaseExtension.getTableID("SELECT * FROM country WHERE CountryID = '" + tfSelect.getText() +"'")[0];		
				Supplier su = Supplier.getInstance();
				su.initCountry((MyCountry) country);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				su.enableSupplier();
			}	
					
			else if (type ==  SearchType.SUPPLIERPAYMENT){
				
				
				NewTirDataBase payment = (MyPayment) DatabaseExtension.getTableID("SELECT * FROM payment WHERE paymentID = '" + tfSelect.getText() +"'")[0];		
				Supplier su = Supplier.getInstance();
				su.initPayment((MyPayment) payment);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				su.enableSupplier();
			}	
					
			else if (type ==  SearchType.SUPPLIERTAX){
				
				
				NewTirDataBase tax = (MyTax) DatabaseExtension.getTableID("SELECT * FROM tax WHERE taxID = '" + tfSelect.getText() +"'")[0];		
				Supplier su = Supplier.getInstance();
				su.initTax((MyTax) tax);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				su.enableSupplier();
			}		else if (type ==  SearchType.CLIENT){
				
				
				//String query = "Select AddresseeID, addressee.name , address, postcode, town, province, addressee.countryID, country.name, phonenumber, fax, nif, contact, email from addressee, country where addressee.countryid = country.countryID and addresseeID = '" + tfSelect.getText() +"'";
				String query = "SELECT * FROM client WHERE clientID = '" + tfSelect.getText() +"'";
				//System.out.println(query);
				//System.out.println("This is the report for addressee");
				NewTirDataBase client = (MyClient) DatabaseExtension.getTableID(query)[0];		
				Client ci = Client.getInstance();
				ci.init((MyClient) client);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				ci.enableClient();
				
						}
			else if (type ==  SearchType.CLIENTCOUNTRY){
				

				NewTirDataBase country = (MyCountry) DatabaseExtension.getTableID("SELECT * FROM country WHERE CountryID = '" + tfSelect.getText() +"'")[0];		
				Client ci = Client.getInstance();
				ci.initCountry((MyCountry) country);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				ci.enableClient();
			}	
					
			else if (type ==  SearchType.CLIENTPAYMENT){
				
				
				NewTirDataBase payment = (MyPayment) DatabaseExtension.getTableID("SELECT * FROM payment WHERE paymentID = '" + tfSelect.getText() +"'")[0];		
				Client ci = Client.getInstance();
				ci.initPayment((MyPayment) payment);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				ci.enableClient();
			}	
					
			else if (type ==  SearchType.CLIENTTAX){
				
				
				NewTirDataBase tax = (MyTax) DatabaseExtension.getTableID("SELECT * FROM tax WHERE taxID = '" + tfSelect.getText() +"'")[0];		
				Client ci = Client.getInstance();
				ci.initTax((MyTax) tax);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				ci.enableClient();
			}	
					
			else if (type ==  SearchType.DESTINATION){
				
				
				NewTirDataBase destination = (MyDestination) DatabaseExtension.getTableID("SELECT * FROM destination WHERE destinationID = '" + tfSelect.getText() +"'")[0];		
				Destination de = Destination.getInstance();
				de.init((MyDestination) destination);
				SearchWindow s = SearchWindow.getInstance();
				s.enableSearchWindow();
				s.dispose();
				thisFrame.dispose();
				de.enableDestination();
			}
					
			else if (type ==  SearchType.DESTINATIONCOUNTRY){
				
				
	NewTirDataBase country = (MyCountry) DatabaseExtension.getTableID("SELECT * FROM country WHERE CountryID = '" + tfSelect.getText() +"'")[0];		
	Destination de = Destination.getInstance();
	de.initCountry((MyCountry) country);
	SearchWindow s = SearchWindow.getInstance();
	s.enableSearchWindow();
	s.dispose();
	thisFrame.dispose();
	de.enableDestination();
			}	
			else if (type ==  SearchType.SHIPMENT){
				
				
				NewTirDataBase shipment = (MyShipment) DatabaseExtension.getTableID("SELECT * FROM shipment WHERE ShipmentID = '" + tfSelect.getText() +"'")[0];		
				Shipment spmnt = Shipment.getInstance();
				spmnt.init((MyShipment) shipment);
				SearchWindow s = SearchWindow.getInstance();
				ShipmentSearchOption shipmentSearchOption = ShipmentSearchOption.getInstance();
				s.enableSearchWindow();
				s.dispose();
				shipmentSearchOption.enableShipmentSearchOption();
				shipmentSearchOption.dispose();
				thisFrame.dispose();
				spmnt.enableShipment();
						}	
else if (type ==  SearchType.SHIPMENTCLIENT){
				
				
	
	String query = "SELECT * FROM client WHERE clientID = '" + tfSelect.getText() +"'";
	NewTirDataBase client = (MyClient) DatabaseExtension.getTableID(query)[0];	
	Shipment spmnt = Shipment.getInstance();
	spmnt.initClient((MyClient) client);
	SearchWindow s = SearchWindow.getInstance();
	s.enableSearchWindow();
	s.dispose();
	thisFrame.dispose();
	spmnt.enableShipment();
	
						}	
					
else if (type ==  SearchType.SHIPMENTADDRESSEE){
				
				
	
	String query = "SELECT * FROM addressee WHERE addresseeID = '" + tfSelect.getText() +"'";
	NewTirDataBase addressee = (MyAddressee) DatabaseExtension.getTableID(query)[0];	
	Shipment spmnt = Shipment.getInstance();
	spmnt.initAddressee((MyAddressee) addressee);
	SearchWindow s = SearchWindow.getInstance();
	s.enableSearchWindow();
	s.dispose();
	thisFrame.dispose();
	spmnt.enableShipment();
	
						}	
					
else if (type ==  SearchType.SHIPMENTDESTINATION){
				
				
	
	String query = "SELECT * FROM destination WHERE destinationID = '" + tfSelect.getText() +"'";
	NewTirDataBase destination = (MyDestination) DatabaseExtension.getTableID(query)[0];	
	Shipment spmnt = Shipment.getInstance();
	spmnt.initDestination((MyDestination) destination);
	SearchWindow s = SearchWindow.getInstance();
	s.enableSearchWindow();
	s.dispose();
	thisFrame.dispose();
	spmnt.enableShipment();
	
						}	
					
else if (type ==  SearchType.SHIPMENTINCOTERM){
				
				
	
	String query = "SELECT * FROM incoterm WHERE incotermID = '" + tfSelect.getText() +"'";
	NewTirDataBase incoterm = (MyIncoterm) DatabaseExtension.getTableID(query)[0];	
	Shipment spmnt = Shipment.getInstance();
	spmnt.initIncoterm((MyIncoterm) incoterm);
	SearchWindow s = SearchWindow.getInstance();
	s.enableSearchWindow();
	s.dispose();
	thisFrame.dispose();
	spmnt.enableShipment();
	
						}	
else if (type ==  SearchType.TRANSPORT){
				
				
	
	String query = "SELECT * FROM transport WHERE transportID = '" + tfSelect.getText() +"'";
	NewTirDataBase transport = (MyTransport) DatabaseExtension.getTableID(query)[0];	
	Transport trans = Transport.getInstance();
	trans.init((MyTransport) transport);
	SearchWindow s = SearchWindow.getInstance();
	s.enableSearchWindow();
	s.dispose();
	thisFrame.dispose();
	trans.enableTransport();
	
						}	
else if (type ==  SearchType.TRANSPORTDESTINATION){
				
				
	
	String query = "SELECT * FROM destination WHERE destinationID = '" + tfSelect.getText() +"'";
	NewTirDataBase destination = (MyDestination) DatabaseExtension.getTableID(query)[0];	
	Transport trans = Transport.getInstance();
	trans.initDestination((MyDestination) destination);
	SearchWindow s = SearchWindow.getInstance();
	s.enableSearchWindow();
	s.dispose();
	thisFrame.dispose();
	trans.enableTransport();
	
						}	
					
else if (type ==  SearchType.TRANSPORTSUPPLIER){
				
				
	
	String query = "SELECT * FROM supplier WHERE supplierID = '" + tfSelect.getText() +"'";
	NewTirDataBase supplier = (MySupplier) DatabaseExtension.getTableID(query)[0];	
	Transport trans = Transport.getInstance();
	trans.initSupplier((MySupplier) supplier);
	SearchWindow s = SearchWindow.getInstance();
	s.enableSearchWindow();
	s.dispose();
	thisFrame.dispose();
	trans.enableTransport();
	
						}	
					
else if (type ==  SearchType.TRANSPORTCOUNTRY){
				
				
	
	String query = "SELECT * FROM country WHERE countryID = '" + tfSelect.getText() +"'";
	NewTirDataBase country = (MyCountry) DatabaseExtension.getTableID(query)[0];	
	Transport trans = Transport.getInstance();
	trans.initCountry((MyCountry) country);
	SearchWindow s = SearchWindow.getInstance();
	s.enableSearchWindow();
	s.dispose();
	thisFrame.dispose();
	trans.enableTransport();
	
						}	
else if (type ==  SearchType.ADDSHIPMENTTRANSPORT){
	
	
	String query = "SELECT * FROM transport WHERE transportID = '" + tfSelect.getText() +"'";
	System.out.println("this is the glob variable inside enterData in transport in the searchtype ADDSHIPEMNTRASNPORT " + Glob.type);
	NewTirDataBase trans = (MyTransport) DatabaseExtension.getTableID(query)[0];		
	AddShipment ship = AddShipment.getInstance();
	ship.initTransport((MyTransport) trans);
	ship.setModel();
	SearchWindow s = SearchWindow.getInstance();
	s.enableSearchWindow();
	s.dispose();
	thisFrame.dispose();
	ship.enableAddShipment();
	
			}	
					
else if (type ==  SearchType.ADDSHIPMENTSHIPMENT){
	
	
	NewTirDataBase shipment = (MyShipment) DatabaseExtension.getTableID("SELECT * FROM shipment WHERE ShipmentID = '" + tfSelect.getText() +"'")[0];		
	AddShipment spmnt = AddShipment.getInstance();
	spmnt.initShipment((MyShipment) shipment);
	SearchWindow s = SearchWindow.getInstance();
	ShipmentSearchOption shipmentSearchOption = ShipmentSearchOption.getInstance();
	s.enableSearchWindow();
	s.dispose();
	shipmentSearchOption.enableShipmentSearchOption();
	shipmentSearchOption.dispose();
	thisFrame.dispose();
	spmnt.enableAddShipment();
	
			}	
					
			//set up a new condition for the searchtype right here
					
			System.out.println("intro has been pressed");
			}catch(Exception ex){
				ex.printStackTrace();
				JOptionPane.showMessageDialog(null, "Enter a valid ID, please"); 
				tfSelect.setText("");
				tfSelect.requestFocus(true);
				return;
			}
		}
 }
public void setModel(){
	
	SearchWindow s = SearchWindow.getInstance();
	System.out.println("Setting model for " + Glob.type);
	switch(Glob.type) {
	case COUNTRY:
	model = buildTableModel("SELECT * FROM Country WHERE name LIKE '%" + s.getFieldText() +"%'");
	
	break;
	case PAYMENT:
		model = buildTableModel("SELECT * FROM Payment WHERE name LIKE '%" + s.getFieldText() +"%'");
		
		break;
	case INCOTERM:
		model = buildTableModel("SELECT * FROM Incoterm WHERE name LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case TAX:
		model = buildTableModel("SELECT * FROM Tax WHERE name LIKE '%" + s.getFieldText() +"%'");
	
		break;
		
	case ADDRESSEE:
		model = buildTableModel("SELECT * FROM Addressee WHERE addressee LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case ADDRESSEECOUNTRY:
		model = buildTableModel("SELECT * FROM Country WHERE name LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case SUPPLIER:
		model = buildTableModel("SELECT * FROM supplier WHERE supplier LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case SUPPLIERCOUNTRY:
		model = buildTableModel("SELECT * FROM Country WHERE name LIKE '%" + s.getFieldText() +"%'");

		break;
		
	case SUPPLIERPAYMENT:
		model = buildTableModel("SELECT * FROM payment WHERE name LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case SUPPLIERTAX:
		model = buildTableModel("SELECT * FROM tax WHERE name LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case CLIENT:
		model = buildTableModel("SELECT * FROM client WHERE client LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case CLIENTCOUNTRY:
		model = buildTableModel("SELECT * FROM Country WHERE name LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case CLIENTPAYMENT:
		model = buildTableModel("SELECT * FROM payment WHERE name LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case CLIENTTAX:
		model = buildTableModel("SELECT * FROM tax WHERE name LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case DESTINATION:
		model = buildTableModel("SELECT * FROM destination WHERE name LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case DESTINATIONCOUNTRY:
		model = buildTableModel("SELECT * FROM country WHERE name LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case SHIPMENT:
		
		
		String code = ShipmentSearchOption.getInstance().getFieldCode();
		if (code.equals("1")){
		if (s.getFieldText().isEmpty()){
			
			model = buildTableModel("Select ShipmentID, client.client as client, addressee.addressee as addressee, Invoice, Rdate, DestinationID, Ddate, Currency, Kilos, Volume, IncotermID, shipment.Comment1, shipment.Comment2, transportID from shipment, client, addressee where shipment.clientid = client.clientid and shipment.addresseeid = addressee.addresseeid");
		}else{
			model = buildTableModel("Select ShipmentID, client.client as client, addressee.addressee as addressee, Invoice, Rdate, DestinationID, Ddate, Currency, Kilos, Volume, IncotermID, shipment.Comment1, shipment.Comment2, transportID from shipment, client, addressee Where shipment.clientid = client.clientid and shipment.addresseeid = addressee.addresseeid and client.name like '%" + s.getFieldText() +"%'");
		}
		}else if (code.equals("2")) {
			if (s.getFieldText().isEmpty()){
				model = buildTableModel("Select ShipmentID, client.client as client, addressee.addressee as addressee, Invoice, Rdate, DestinationID, Ddate, Currency, Kilos, Volume, IncotermID, shipment.Comment1, shipment.Comment2, transportID from shipment, client, addressee where shipment.clientid = client.clientid and shipment.addresseeid = addressee.addresseeid");
			}else{
				model = buildTableModel("Select ShipmentID, client.client as client, addressee.addressee as addressee, Invoice, Rdate, DestinationID, Ddate, Currency, Kilos, Volume, IncotermID, shipment.Comment1, shipment.Comment2, transportID from shipment, client, addressee Where shipment.clientid = client.clientid and shipment.addresseeid = addressee.addresseeid and addressee.name like '%" + s.getFieldText() +"%'");
			}
			
		}
		break;
		
	case SHIPMENTCLIENT:
		model = buildTableModel("SELECT * FROM client WHERE client LIKE '%" + s.getFieldText() +"%'");
		
		break;
		
	case SHIPMENTADDRESSEE:
		model = buildTableModel("SELECT * FROM Addressee WHERE addressee LIKE '%" + s.getFieldText() +"%'");
		break;
		
	case SHIPMENTDESTINATION:
		model = buildTableModel("SELECT * FROM destination WHERE name LIKE '%" + s.getFieldText() +"%'");
		break;
		
	case SHIPMENTINCOTERM:
		model = buildTableModel("SELECT * FROM Incoterm WHERE name LIKE '%" + s.getFieldText() +"%'");
		break;
		
	case TRANSPORT:
		model = buildTableModelString("SELECT * FROM transport WHERE TransportID LIKE '%" + s.getFieldText() +"%'");
	break;
	case TRANSPORTDESTINATION:
		model = buildTableModel("SELECT * FROM destination WHERE name LIKE '%" + s.getFieldText() +"%'");
	break;
	case TRANSPORTSUPPLIER:
		model = buildTableModel("SELECT * FROM supplier WHERE supplier LIKE '%" + s.getFieldText() +"%'");
	break;
	case TRANSPORTCOUNTRY:
		model = buildTableModel("SELECT * FROM country WHERE name LIKE '%" + s.getFieldText() +"%'");
	break;
	case ADDSHIPMENTTRANSPORT:
		model = buildTableModelString("SELECT * FROM transport WHERE transportID LIKE '%" + s.getFieldText() +"%'");
	break;
	case ADDSHIPMENTSHIPMENT:
		String codes = ShipmentSearchOption.getInstance().getFieldCode();
		if (codes.equals("1")){
		if (s.getFieldText().isEmpty()){
			
			model = buildTableModel("Select ShipmentID, client.client as client, addressee.addressee as addressee, Invoice, Rdate, DestinationID, Ddate, Currency, Kilos, Volume, IncotermID, shipment.Comment1, shipment.Comment2, transportID from shipment, client, addressee where shipment.clientid = client.clientid and shipment.addresseeid = addressee.addresseeid");
		}else{
			model = buildTableModel("Select ShipmentID, client.client as client, addressee.addressee as addressee, Invoice, Rdate, DestinationID, Ddate, Currency, Kilos, Volume, IncotermID, shipment.Comment1, shipment.Comment2, transportID from shipment, client, addressee Where shipment.clientid = client.clientid and shipment.addresseeid = addressee.addresseeid and client.client like '%" + s.getFieldText() +"%'");
		}
		}else if (codes.equals("2")) {
			if (s.getFieldText().isEmpty()){
				model = buildTableModel("Select ShipmentID, client.client as client, addressee.addressee as addressee, Invoice, Rdate, DestinationID, Ddate, Currency, Kilos, Volume, IncotermID, shipment.Comment1, shipment.Comment2, transportID from shipment, client, addressee where shipment.clientid = client.clientid and shipment.addresseeid = addressee.addresseeid");
			}else{
				model = buildTableModel("Select ShipmentID, client.client as client, addressee.addressee as addressee, Invoice, Rdate, DestinationID, Ddate, Currency, Kilos, Volume, IncotermID, shipment.Comment1, shipment.Comment2, transportID from shipment, client, addressee Where shipment.clientid = client.clientid and shipment.addresseeid = addressee.addresseeid and addressee.addressee like '%" + s.getFieldText() +"%'");
			}
			
		}

	break;
	
		//set up next condition here
	}
	table.setModel(model);
}
public void setTitle(){
switch(Glob.type) {
	case COUNTRY:
		setTitle("Country Results");
		title = "Country Results";
		break;
	case PAYMENT:
		setTitle("Payment Results");
		title = "Payment Results";
		break;
	case INCOTERM:
		setTitle("Incoterm Results");
		title = "Incoterm Results";
		break;
		
	case TAX:
		setTitle("Tax Results");
		title = "Tax Results";
		break;
		
	case ADDRESSEE:
		setTitle("Addressee Results");
		title = "Addressee Results";
		break;
		
	case ADDRESSEECOUNTRY:
		setTitle("Country Results");
		title = "Country Results";
		break;
		
	case SUPPLIER:
		setTitle("Supplier Results");
		title = "Supplier Results";
		break;
		
	case SUPPLIERCOUNTRY:
		setTitle("Country Results");
		title = "Country Results";
		break;
		
	case SUPPLIERPAYMENT:
		setTitle("Payment Results");
		title = "Payment Results";
		break;
		
	case SUPPLIERTAX:
		setTitle("Tax Results");
		title = "Tax Results";
		break;
		
	case CLIENT:
		setTitle("Client Results");
		title = "Client Results";
		break;
		
	case CLIENTCOUNTRY:
		setTitle("Country Results");
		title = "Country Results";
		break;
		
	case CLIENTPAYMENT:
		setTitle("Payment Results");
		title = "Payment Results";
		break;
		
	case CLIENTTAX:
		setTitle("Tax Results");
		title = "Tax Results";
		break;
		
	case DESTINATION:
		setTitle("Destination Results");
		title = "Destination Results";
		break;
		
	case DESTINATIONCOUNTRY:
		setTitle("Country Results");
		title = "Country Results";
		break;
		
	case SHIPMENT:
		setTitle("Shipment Results");
		title = "Shipment Results";
		break;
		
	case SHIPMENTCLIENT:
		setTitle("Client Results");
		title = "Client Results";
		break;
		
	case SHIPMENTADDRESSEE:
		setTitle("Addressee Results");
		title = "Addressee Results";
		break;
		
	case SHIPMENTDESTINATION:
		setTitle("Destination Results");
		title = "Destination Results";
		break;
	case SHIPMENTINCOTERM:
		setTitle("Incoterm Results");
		title = "Incoterm Results";
		break;
		
	case TRANSPORT:
		setTitle("Transport Results");
		title = "Transport Results";
		break;

	case TRANSPORTDESTINATION:
		setTitle("Destination Results");
		title = "Destination Results";
	break;
	case TRANSPORTSUPPLIER:
		setTitle("Supplier Results");
		title = "Supplier Results";
	break;
	case TRANSPORTCOUNTRY:
		setTitle("Country Results");
		title = "Country Results";
	break;
	case ADDSHIPMENTTRANSPORT:
		setTitle("Transport Results");
		title = "Transport Results";
	break;
	case ADDSHIPMENTSHIPMENT:
		setTitle("Shipment Results");
		title = "Shipment Results";
	break;
		//set up next condition here
}
	}
 //method to exit from the screen
 public void escape(SearchType type){
	 	thisFrame.dispose();
		SearchWindow s = SearchWindow.getInstance();
		s.toFront();
		s.enableSearchWindow();
		s.setSearchBarFocus();
 }
}